package com.qiche.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.dto.Voucher1Dto;
import com.qiche.dto.VoucherDto;
import com.qiche.mapper.VoucherMapper;
import com.qiche.model.Agent;
import com.qiche.model.User;
import com.qiche.model.Voucher;
import com.qiche.model.VoucherRepository;
import com.qiche.service.AgentService;
import com.qiche.service.VoucherRepositoryService;
import com.qiche.service.VoucherService;
import com.qiche.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("voucherService")
public class VoucherServiceImpl extends BaseService<Voucher> implements VoucherService {

    @Autowired
    private AgentService agentService;

    @Resource
    private VoucherMapper voucherMapper;

    @Override
    public PageInfo<Voucher> selectByPage(Voucher voucher, int page, int limit) {
        Example example = new Example(Voucher.class);
        Example.Criteria criteria = example.createCriteria();
        System.out.println(voucher.getCode());
        if (StringUtils.isNotBlank(voucher.getCode())) {
            criteria.andLike("code", "%" + voucher.getCode() + "%");
        }
        if (voucher.getMerchantId()!=null) {
            criteria.andEqualTo("merchantId", voucher.getMerchantId());
        }
        PageHelper.startPage(page, limit);
        List<Voucher> voucherList = selectByExample(example);
        return new PageInfo<>(voucherList);
    }

    @Override
    public Voucher selectByCode(String code) {
        Example example = new Example(Voucher.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        List<Voucher> voucherList = selectByExample(example);
        if (!voucherList.isEmpty()) {
            return voucherList.get(0);
        }
        return null;
    }

    @Override
    public Result verify(String code) {
        User currentUser = UserUtils.getCurrentUser();
        Agent agent = agentService.selectByAccountId(currentUser.getId());
        if (agent == null) {
            return Result.createByErrorMessage("不支持的操作");
        }

        if (StringUtils.isBlank(code)) {
            return Result.createByErrorMessage("参数错误");
        }
        Voucher voucher = selectByCode(code);
        if (voucher == null) {
            return Result.createByErrorMessage("不存在的代金券");
        }
        if (voucher.getIsWriteoff() == 1) {
            return Result.createByErrorMessage("该代金券已被使用");

        }
        if (voucher.getIndefinite() == 0) {
            if (voucher.getDueTime().before(new Date())) {
                return Result.createByErrorMessage("代金券已过期");
            }
        }
        if (voucher.getAgentId() != agent.getId()) {
            return Result.createByErrorMessage("请在指定的代理机构使用");
        }

        voucher.setIsWriteoff(1);
        voucher.setWriteOffTime(new Date());
        int isUpdate = updateNotNull(voucher);
        if (isUpdate <= 0) {
            return Result.createByErrorMessage("核销失败");
        }
        return Result.createBySuccessMessage("核销成功");
    }

    @Override
    public List<Voucher1Dto> selectByMerchantId(Long merchantId, Long storeMemberId) {
        return voucherMapper.selectByMerchantId(merchantId,storeMemberId);
    }

}
